using System.Collections;
using Concurso.Clases;
using Concurso.Clases.TreeViewManage;
using Curriculum.Metadatos.ClasesMetadatos;
using Curriculum.Services;
using FunkeLab;

namespace ConcursoCV.UI.WebUserControls.AdmVersionesCV
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		Summary description for ArbolVersionCV.
	/// </summary>
	public class ArbolVersionCV : TreeViewControl
	{
		protected FunkeLab.MyTreeView treeView;
		protected System.Web.UI.WebControls.Button btnAgregar;
		protected System.Web.UI.WebControls.Button btnModificar;
		protected System.Web.UI.WebControls.Label lblError;

		#region - Public Events
		
		//
		public event EventHandler Add;
		public event EventHandler Modificar;
		// Tree
		public event EventHandler TreeViewNodeClick;
		public event EventHandler TreeViewNodeOnDemand;
		public event EventHandler TreeViewOnNodeClose;

		#endregion - Public Events

		private void Page_Load(object sender, System.EventArgs e)
		{
			lblError.Text = string.Empty;
			MyTreeView.ImagesFolder = "img/tree/";			
			if(!this.IsPostBack)
			{
				InitalizeTreeView();
			}
		}
		private IList GetRootNodes()
		{
			VersionMetadatosService svc = new VersionMetadatosService();
			IList rootNodes = svc.ObtenerNodosRaiz();
			return rootNodes;
		}

		protected void LoadNodes( MyTreeViewItem parentItem, IList nodes, bool loadOnExpand) 
		{
			foreach( AntecedenteMetadato node in nodes ) 
			{
				MyTreeViewItem item = new MyTreeViewItem();
				item.Text = node.Nombre;
				item.Value = node.Id;
				item.LoadOnExpand = loadOnExpand;
				parentItem.Items.Add( item );
			}
		}

		public void InitalizeTreeView() 
		{
			RootItem.Items.Clear();
			IList rootNodes = GetRootNodes();				
			LoadNodes( RootItem, rootNodes, true );
		}

		#region - PaintSelectedNode -


		public void UndoPaintSelectedNode(ArrayList path)
		{
			treeView.GetNode(path).BackColor = System.Drawing.Color.White;
		}

		#endregion - PaintSelectedNode -

		private void PaintNodeAccordingToAction() 
		{			
			FunkeLab.MyTreeViewItem item = treeView.GetNode(SelectedPathNode);
			PaintSelectedNode(treeView, item);
		}

		private FunkeLab.MyTreeViewItem GetSelectedItem() 
		{
			return treeView.GetNode(SelectedPathNode);
		}

		public void RefreshTreeView() 
		{
			try 
			{
				FunkeLab.MyTreeViewItem item = GetSelectedItem();
				item.Items.Clear();
				VersionMetadatosService svc = new VersionMetadatosService();
				IList rootNodes = null;				
				if( item.Value == "ROOT")
					rootNodes  = svc.ObtenerNodosRaiz();
				else
				{
					item.Text = svc.ObtenerAntecedenetMetadato(this.SelectedNode).Nombre;
					rootNodes = svc.ObtenerNodosHijo(SelectedNode.ToString());
				}				
				LoadNodes( item , rootNodes, true);
				PaintNodeAccordingToAction();
				item.Open = true;
			}
			catch(Exception exc)
			{
				lblError.Text = exc.Message;
			}
		}

		private void RefreshItem()
		{
			MyTreeViewItem  Item = treeView.GetNode(SelectedPathNode).Owner;
			SelectedNode = Item.Value;
			SelectedPathNode = treeView.GetPathNode(Item);	
		}

		#region - properties -

		protected override MyTreeViewItem RootItem 
		{
			get {return treeView.Items[0];}
		}

		public object ParentOrgNodeId
		{
			get{ return ViewState["_parentOrgNodeId"];}	
			set{ ViewState["_pathOrgNodeId"] = value;}
		}

		public object SelectedNode
		{
			get{ return ViewState["_selectedNode"];}	
			set{ ViewState["_selectedNode"] = value;}			
		}

		public string ErrorMessage
		{
			get{ return lblError.Text;}
			set{ lblError.Text = value;}		
		}

		public bool AgregarEnabled
		{
			get{ return btnAgregar.Enabled;}
			set{ btnAgregar.Enabled = value;}
		}
		public bool ModificarEnabled
		{
			get{ return btnModificar.Enabled;}
			set{ btnModificar.Enabled = value;}
		}
	
		public override MyTreeView Tree
		{
			get
			{
				return this.treeView;
			}
		}


		#endregion - properties -

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			MyInitializeComponent();
			base.OnInit(e);
		}
		
		private void MyInitializeComponent()
		{
			this.treeView.OnNodeClick +=new FunkeLab.MyTreeView.OnNodeClickEventHandler(treeView_OnNodeClick);
			this.treeView.OnNodeClose +=new FunkeLab.MyTreeView.OnNodeCloseEventHandler(treeView_OnNodeClose);
			this.treeView.OnNodeLoadOnDemand +=new FunkeLab.MyTreeView.OnNodeLoadOnDemandEventHandler(treeView_OnNodeLoadOnDemand);
		}
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnAgregar.Click += new System.EventHandler(this.btnAgregar_Click);
			this.btnModificar.Click += new System.EventHandler(this.btnModificar_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		protected override void treeView_OnNodeClick(ref MyTreeViewItem Item)
		{
			this.btnAgregar.Enabled = true;
			this.btnModificar.Enabled = true;
			SelectedPathNode = treeView.GetPathNode(Item);
			SelectedNode = Item.Value;
			PaintSelectedNode( treeView,Item );
			this.RaiseTreeViewNodeClickEvent( Item );				
		}

		private void treeView_OnNodeClose(ref MyTreeViewItem Item)
		{
			if ( !( Item.Value == RootItem.Value )) 
			{
				Item.Items.Clear();
			}
			else
			{
				if(SelectedPathNode != null) 
					UndoPaintSelectedNode(SelectedPathNode);
			}

			this.RaiseTreeViewOnNodeClose();		
		}

		protected override void treeView_OnNodeLoadOnDemand(ref MyTreeViewItem Item)
		{
			ErrorMessage = string.Empty;			
			if( Item != RootItem )
			{
				bool loadOnExpand = true;
				VersionMetadatosService svc = new VersionMetadatosService();
				IList nodes = svc.ObtenerNodosHijo(Item.Value);
				if(nodes.Count>0)
				{
					LoadNodes( Item , nodes, loadOnExpand);
				}				
			}
		}
		#region - RaiseEvents -

		protected void RaiseTreeViewNodeClickEvent ( MyTreeViewItem item ) 
		{
			if (this.TreeViewNodeClick != null)
				this.TreeViewNodeClick(this, new TreeViewEventArgs(item) );		
		}

		protected void RaiseTreeViewNodeOnDemandEvent (MyTreeViewItem node) 
		{
			if (this.TreeViewNodeOnDemand != null)
				this.TreeViewNodeOnDemand(this, null);		
		}

		protected void RaiseTreeViewOnNodeClose () 
		{
			if (this.TreeViewOnNodeClose != null)
				this.TreeViewOnNodeClose(this, null);		
		}
		
		protected void RaiseAdd () 
		{
			if (this.Add != null)
				this.Add(this, null);		
		}

		protected void RaiseModificar () 
		{
			if (this.Modificar != null)
				this.Modificar(this, null);		
		}

		#endregion - RaiseEvents -

		private void btnAgregar_Click(object sender, System.EventArgs e)
		{
			if ( this.SelectedNode != null)
				this.RaiseAdd();
			else
				lblError.Text = "Seleccione un nodo";
		}

		private void btnModificar_Click(object sender, System.EventArgs e)
		{			
			if ((SelectedNode != null) && (!SelectedNode.ToString().Equals("ROOT")))
			{
				this.RaiseModificar();
			}
		}
	}
}
